<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="预警名称：" prop="typeName" :rules="[
      {
        required: true,
        message: '预警类型名称不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.typeName"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="预警类型：" prop="parentId" :rules="[
      {
        required: true,
        message: '请选择预警类型',
        trigger: 'change',
      },
    ]">
            <el-select v-model="form.parentId" @change="selectChang">
              <el-option v-for="item in alertList" :key="item.id" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.parentId === '1'">
          <el-form-item label="最大年龄：" prop="maxAge" :rules="[
      {
        required: true,
        message: '请输入最大年龄',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.maxAge"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.parentId === '1'">
          <el-form-item label="最小年龄：" prop="minAge" :rules="[
      {
        required: true,
        message: '请输入最小年龄',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.minAge"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="form.parentId === '2'">
          <el-form-item label="最多跑天数：" prop="timeDay" :rules="[
      {
        required: true,
        message: '请输入最多跑天数',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.timeDay"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.parentId === '2'">
          <el-form-item label="最多跑次数：" prop="timeCount" :rules="[
      {
        required: true,
        message: '请输入最多跑次数',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.timeCount"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24" v-if="form.parentId === '3'">
          <el-form-item label="办理时限（分钟）：" prop="duration" :rules="[
      {
        required: true,
        message: '请输入办理时限',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.duration"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24" v-if="form.parentId === '4'">
          <el-form-item label="最多人数：" prop="traffic" :rules="[
      {
        required: true,
        message: '请输入最多人数',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.traffic"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否计算工作人员：" prop="isCountEmployee">
            <el-select disabled v-model="form.isCountEmployee" placeholder="请选择">
              <el-option value="1" label="计算工作人员"></el-option>
              <el-option value="0" label="不计算工作人员"></el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="状态启用：" prop="enabled" :rules="[
      {
        required: true,
        message: '请选择状态',
        trigger: 'change',
      },
    ]">
            <el-select v-model="form.enabled">
              <el-option value="1" label="启用"></el-option>
              <el-option value="0" label="不启用"></el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存
                </el-button>
              </div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { addWarning, editWarning } from "@/api/warning";

export default {
  props: {
    modeType: {
      type: Number,
      default: 0,
    },
    snapUserInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    typeOfAlertList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  watch: {
    snapUserInfo: {
      handler(val) {
        console.log(val);
        for(let item in this.snapUserInfo) {
        this.form[item] = val[item]?.toString();
      }
      },
      deep: true
    },
  },
  mounted() {
    if(this.modeType !== 1) {
      for(let item in this.snapUserInfo) {
        this.form[item] = this.snapUserInfo[item]?.toString();
      }
      console.log(this.form);
    }
  },
  data() {
    return {
      alertList: [
        {
          id: '1',
          name: '关怀预警'
        },
        {
          id: '2',
          name: '最多跑一次'
        },
        {
          id: '3',
          name: '时长预警'
        },
        {
          id: '4',
          name: '大流量预警'
        }
      ],
      alertListInfo: {
        1: '关怀预警',
        2: '最多跑一次',
        3: '时长预警',
        4: '大流量预警'
      },
      warnList: [
        {
          id: '0',
          name: "不预警",
        },
        {
          id: '1',
          name: "出现则预警",
        },
        {
          id: '2',
          name: "月末预警",
        },
      ],
      value1: 1,
      form: {
        typeName: "",
        parentId: '',
        maxAge: '',
        minAge: '',
        timeDay: '',
        timeCount: '',
        duration: '',
        traffic: '',
        isCountEmployee: '0',
        parentName: '',
        enabled:'1',

      },
      loading: false,
    };
  },

  methods: {
    async addWarningType() {
      let res = await addWarning(this.form);
      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });

        this.loading = false;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.parentName = this.alertListInfo[this.form.parentId]
          if (this.modeType == 1) {
            this.addWarningType();
          } else {
            this.editWarningType();
          }
        }
      });
    },
    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
    async editWarningType() {
      await editWarning(this.form).then((res) => {
        if (res.code == 200) {
          this.$emit("onClose", {
            isClose: false,
          });
          this.$emit("onUpdate");
        } else {
          this.$message.error(res.msg)
        }
        this.loading = false;
      });
    },
    selectChang(e) {
      this.form.parentId = e;
    }
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
::v-deep .is-disabled{
  opacity: 0.4;
  background-color: rgba(23, 24, 26, 0.1);
}

</style>
